package com.example.fragment;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.example.bean.NewFragmentBean;
import com.example.fruit.MyApplication;
import com.example.fruit.R;
import com.example.utils.DBUtils;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.List;


public class CartFragment extends Fragment {

    private CursorAdapter adapter;
    private ListView listView;
    private List<NewFragmentBean> data;
    private MyApplication app;
    private Cursor cursor;
    private SQLiteDatabase db;
    private int itemPosition;
    private DBUtils dbUtils;
    private BitmapUtils bitmap;
    private  TextView maths;
    @ViewInject(R.id.total_cart)
    private  TextView addup;
    private float total=0;
    private View views;


    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
         views = inflater.inflate(R.layout.fragment_cart, container, false);

        ViewUtils.inject(this, views);
        return views;



    }

    private void initView() {

        Cursor cursor = db.rawQuery("select * from t_cart", null);
        while(cursor.moveToNext()){
            total+=cursor.getInt(6)*cursor.getFloat(5);

        }
        addup.setText("￥"+ total);

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        app = MyApplication.getApplication();
        dbUtils = app.getDbUtils();
        db = dbUtils.getReadableDatabase();
        bitmap = new BitmapUtils(getActivity());
        cursor = db.rawQuery("select * from t_cart", null);
        listView = (ListView) views.findViewById(R.id.listView_cart);
        adapter = new CursorAdapter(getActivity(), cursor) {

            @Override
            public View newView(Context context, Cursor cursor, ViewGroup parent) {
                LayoutInflater inflter = LayoutInflater.from(context);
                View view = inflter.inflate(R.layout.item_cart, null);
                return view;
            }

            @Override
            public void bindView(View view, Context context, Cursor cursor) {
                TextView nameTv = (TextView) view.findViewById(R.id.title_cart);
                nameTv.setText(cursor.getString(2));
                String imgPath = cursor.getString(4);
                ImageView iv = (ImageView) view.findViewById(R.id.img_cart);
                bitmap.display(iv, imgPath);
                TextView contentTv = (TextView) view.findViewById(R.id.title2_cart);
                contentTv.setSingleLine(true);
                contentTv.setText(cursor.getString(3));
                TextView price = (TextView) view.findViewById(R.id.price1_cart);
                price.setText("￥" + cursor.getFloat(5));
                TextView maths= (TextView) view.findViewById(R.id.mathId);
                maths.setText(""+cursor.getInt(6));

            }
        };

        listView.setAdapter(adapter);

        registerForContextMenu(listView);


        query();

        initView();

    }

    // 查询
    private void query() {
        SQLiteDatabase db = app.getDbUtils().getReadableDatabase();
        Cursor clsCurosr = db.rawQuery("select * from t_cart", null);

        adapter.swapCursor(clsCurosr);
    }



}


